<template>
  <div>
    <el-button @click="getEditVal">取值</el-button>
    <yu-ace-editor :data="editorStr" lang="json" v-model:value="editorStr" @handleCells="handleCells" @handleFormat="handleFormat"></yu-ace-editor>
    <div style="padding: 8px 0">
      <el-button type="primary" @click="handleCells">压缩</el-button>
      <el-button type="primary" @click="handleFormat">格式化</el-button>
    </div>
    <div>sql</div>
    <yu-ace-editor :data="data.sql" lang="sql" v-model:value="data.sql"></yu-ace-editor>
    <b-ace-editor v-model="data.sql" height="300" lang="sql" wrap></b-ace-editor>
  </div>
</template>
<script setup>
  import { reactive, ref, toRefs, onMounted, getCurrentInstance } from 'vue';
  import * as ace from 'brace';
  import 'brace/ext/emmet';
  import 'brace/ext/language_tools';
  import 'brace/mode/json';
  import 'brace/snippets/json';
  import 'brace/theme/chrome';
  import 'brace/theme/xcode';
  import 'brace/theme/clouds';
  import 'brace/theme/crimson_editor';
  import 'brace/theme/sqlserver';
  import 'brace/theme/github';
  import 'brace/theme/textmate';
  import 'brace/theme/tomorrow';
  import 'brace/theme/solarized_light';
  import 'brace/theme/cobalt';
  import 'brace/theme/dracula';
  import 'brace/theme/monokai';
  import 'brace/theme/solarized_dark';
  import 'brace/theme/terminal';
  import 'brace/theme/vibrant_ink';
  const readonly = ref(false);
  const theme = ref('solarized_light');
  const fontSize = ref(12);
  const getEditVal = () => {
    alert(editorStr.value);
  };
  const data = reactive({
    sql: '',
  });
  data.sql = "SELECT * from sys_user where user_name = 'test'";
  const sqlStr = ref("SELECT * from sys_user where user_name = 'test'");
  const themeList = ref([
    'chrome',
    'xcode',
    'clouds',
    'crimson_editor',
    'sqlserver',
    'github',
    'textmate',
    'tomorrow',
    'solarized_light',
    'cobalt',
    'dracula',
    'monokai',
    'solarized_dark',
    'terminal',
    'vibrant_ink',
  ]);

  const editorStr = ref(
    JSON.stringify(
      {
        title: '测试json数据233',
        child: [
          {
            title: '子项名称1',
            desc: '子项描述1',
          },
          {
            title: '子项名称2',
            desc: '子项描述2',
          },
        ],
      },
      null,
      2
    )
  );
  const handleCells = () => {
    editorStr.value = JSON.stringify(JSON.parse(editorStr.value), null, 0);
  };
  // 压缩
  const handleZip = () => {
    jsonStr.value = JSON.stringify(JSON.parse(jsonStr.value), null, 0);
  };
  //格式化
  const handleFormat = () => {
    editorStr.value = JSON.stringify(JSON.parse(editorStr.value), null, 2);
  };
</script>
